<!-- 任务类型弹框 -->
<form id="taskAreas-form" lay-filter="taskAreas-form" class="layui-form model-form">
    <input name="id" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">任务类型名称</label>
        <div class="layui-input-block">
            <input name="areaName" placeholder="请输入任务类型名称" type="text" class="layui-input" maxlength="50"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">任务类型Value</label>
        <div class="layui-input-block">
            <input name="areaValue" placeholder="请输入任务类型Value" type="text" class="layui-input"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否隐藏</label>
        <div class="layui-input-block">
            <input type="radio" name="isHidden" value="0" title="否" checked/>
            <input type="radio" name="isHidden" value="1" title="是"/>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">上级任务类型</label>
        <div class="layui-input-block">
            <select name="parentId" id="parentId">
                <option value="-1">顶级目录</option>
            </select>
        </div>
    </div>

    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
        <button class="layui-btn" lay-filter="taskAreas-form-submit" lay-submit>保存</button>
    </div>
</form>

<script>
    layui.use(['layer', 'admin', 'form', 'formSelects'],function () {
        var layer = layui.layer;
        var admin = layui.admin;
        var form = layui.form;
        var formSelects = layui.formSelects;

		// debugger ;
        form.render('radio');


        // 获取一级任务类型
        layer.load(2);
        admin.req('api-task/taskArea/findTaskArea', {}, function (data) {
            layer.closeAll('loading');
            if (0 == data.code) {

                $.each(data.data,function(index,item){
                    $('#parentId').append(new Option(item.areaName,item.id));//往下拉任务类型里添加元素
                })

                form.render();  //任务类型渲染 把内容加载进去

                // 回显task数据
                var taskAreas = admin.getTempData('t_taskAreas');
                console.log(taskAreas);
                $('#taskAreas-form').attr('method', 'POST');
                if (taskAreas) {

                    form.val('taskAreas-form', taskAreas);
                    $('#taskAreas-form').attr('method', 'POST');
                }
            } else {
                layer.msg('获取一级任务类型', {icon: 2, time: 500});
            }
        }, 'GET');

        // 表单提交事件
        form.on('submit(taskAreas-form-submit)', function (data) {
            layer.load(2);
            var taskAreas = admin.getTempData('t_taskAreas');
            if(!taskAreas){
                admin.req('api-task/taskArea/addTaskArea', JSON.stringify(data.field), function (data) {

                    if (data.code === 0) {
                        layer.closeAll('loading');
                        layer.msg(data.msg, {icon: 1, time: 500});
                        admin.finishPopupCenter();
                    } else {
                        layer.closeAll('loading');
                        layer.msg(data.msg, {icon: 2, time: 500});
                    }
                }, $('#taskAreas-form').attr('method'));
            }else {
                admin.req('api-task/taskArea/updateTaskArea', JSON.stringify(data.field), function (data) {

                    if (data.code === 0) {
                        layer.closeAll('loading');
                        layer.msg(data.msg, {icon: 1, time: 500});
                        admin.finishPopupCenter();
                    } else {
                        layer.closeAll('loading');
                        layer.msg(data.msg, {icon: 2, time: 500});
                    }
                }, $('#taskAreas-form').attr('method'));
            }

            return false;
        });


    });
</script>